package com.example.batch.reader;

import com.example.batch.dto.IdDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/7/12 09:45
 */
@Component
@Slf4j
public class MyFlatFileReader extends FlatFileItemReader {
    public static final String filaPath = "/Users/yuhang/Downloads/ids.txt";

    /**
     * 分隔符文件读取配置
     */
    public MyFlatFileReader() {
        setResource(new PathResource(filaPath));
        setLineMapper(new DefaultLineMapper<IdDto>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{  //一行内容分割默认用','分割
                setNames(new String[]{"id"});
                setComments(new String[]{"==","##"});  //# 为默认的注释符号
                //setLinesToSkip(1);
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper() {{
                setTargetType(IdDto.class);
            }});
        }});
    }

    @Override
    protected Object doRead() throws Exception {
        log.info(" doRead  :{}",super.getCurrentItemCount());
        return super.doRead();
    }
}
